/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.aun.news;

import br.org.groupwareworkbench.core.bd.GenericDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class NewsDAO extends GenericDAO<News> {

    public NewsDAO() {
        super(News.class);
    }

    public void copyFromAUN(Long idInstance) {
        System.out.println("Inicio DA COPIA DA AUN!");

        ConnectionMysql con = null;
        Connection dbCon = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            con = new ConnectionMysql();
            dbCon = con.getConnection();
            stmt = dbCon.prepareStatement("SELECT * FROM materia");
            rs = stmt.executeQuery();
            int i = 0;
            while (rs.next() && i < 30) {
                if (rs.getString("titulo").isEmpty()) {
                    System.out.println("!!!Pulei a matéria " + rs.getString("cod_materia") + " pois ela não tinha titulo!");
                    continue;
                }
                News materia = new News();
                String codMateria = rs.getString("cod_materia");
                String titulo = rs.getString("titulo");
                titulo = titulo.replaceAll("<+[A-Za-z0-9._%+-/\\\"\'= ]+>", " "); // Limpa tags HTML do título.
                String subtitulo = rs.getString("sub_titulo");//.replaceAll("<+[A-Za-z0-9._%+-/ ]+>", " ");
                String corpo = rs.getString("corpo");
                //corpo = (corpo.substring(34)).replaceAll("<+[A-Za-z0-9._%+-/\\\"\'= ]+>", " ");
                String lead = rs.getString("lead");
                String categoria = rs.getString("categoria");
                String relevancia = rs.getString("relevancia");
                String status = rs.getString("status");
                Date dataFecha;
                Date dataEnvia;
                Date dataPublica;

                try {
                    dataFecha = rs.getDate("data_fecha");
                } catch (Exception e) {
                    dataFecha = new Date();
                }

                try {
                    dataEnvia = rs.getDate("data_envia");
                } catch (Exception e) {
                    dataEnvia = new Date();
                }

                try {
                    dataPublica = rs.getDate("data_publica");
                } catch (Exception e) {
                    dataPublica = new Date();
                }

                materia.setCodNews(codMateria);
                materia.setTitle(titulo);
                materia.setSubTitle(subtitulo);
                materia.setBody(corpo);
                materia.setLead(lead);
                materia.setCategory(categoria);
                materia.setRelevance(relevancia);
                materia.setStatus(status);
                materia.setCloseDate(dataFecha);
                materia.setSendDate(dataEnvia);
                materia.setPublishDate(dataPublica);
                materia.setIdInstance(idInstance);

                List<News> newsList = listAll();
                if (!newsList.contains(materia))
                    save(materia, true);
                i++;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("O Banco de Dados original não foi copiado, verifique sua conexão!");
        } finally {
            try {
                if (rs != null) rs.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
            try {
                if (dbCon != null) dbCon.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
            try {
                if (con != null) con.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
        }
    }
}
